package com.neusoft.yuejian_01.model;

import com.neusoft.yuejian_01.base.BaseModel;
import com.neusoft.yuejian_01.javabean.UserBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class UserModel extends BaseModel {


   //用户登录
    public void login(String username,String userpass,final RetrofitListener listener) {
        this.flag = Const.Login;
        UserService userService=retrofit.create(UserService.class);
        Call call = userService.userLogin(username,userpass);
        Callback<UserBean> callback = new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean userBean=response.body();
                listener.onSuccess(userBean,flag);
            }
            @Override
            public void onFailure(Call<UserBean> call, Throwable t)  {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    //用户注册
    public void reg(String et_username, String et_userpass, String et_mobilenum,final RetrofitListener listener) {
        this.flag = Const.Reg;
        UserService userService=retrofit.create(UserService.class);
        Call call = userService.userReg(et_username,et_userpass,et_mobilenum);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean userBean=response.body();
                listener.onSuccess(userBean,flag);
            }
            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail();
            }
            });
    }

    //
    public void userInfo(int u_id,final RetrofitListener listener) {
        this.flag = Const.UserInfo;
        UserService userService=retrofit.create(UserService.class);
        Call call = userService.userInfo(u_id);
        call.enqueue(new Callback<UserBean>()
        {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean userInfo=response.body();
                listener.onSuccess(userInfo,flag);
            }
            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail();
            }
        });
    }
    //修改个人信息
    public void updateInfo(int u_id, String u_name, String u_pass, String u_phone, String u_address, String u_email,final RetrofitListener listener) {
        this.flag = Const.UpdateInfo;
        UserService userService=retrofit.create(UserService.class);
        Call call = userService.updateInfo(u_id,u_name,u_pass,u_phone,u_address,u_email);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                UserBean userBean=response.body();
                listener.onSuccess(userBean,flag);

            }
            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail();
            }
        });
    }

}



